const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_board: false,
    address: '',
    disabled: false,
    loading: false,
    name: '',
    act: 'add',
    tel: '',
    switchActive: false,
    address_text: '',
  },
  switchTab() {
    this.setData({
      switchActive: !this.data.switchActive
    })
  },
  choosAddress: function () {
    this.setData({
      address_board: true
    })
  },
  /**
   * 确认所在地区
   */
  confirmAddress(e) {
    console.log(e);
    this.setData({
      address: e.detail
    })
  },
  chanageName: function (obj) {
    this.data.name = obj.detail.value;
  },
  chanageTel: function (obj) {
    this.data.tel = obj.detail.value;
  },
  chanageAddress: function (obj) {
    this.data.address_text = obj.detail.value;
  },
  /**
   * @xk 保存
   */
  save: function () {
    var that = this,
      name = this.data.name,
      tel = this.data.tel,
      address = this.data.address,
      address_text = this.data.address_text,
      is_default = 0,
      act = this.data.act,
      address_id = this.data.address_id ? this.data.address_id : 0,
      cart_ids = app.globalData.cart_id,
      user_id = wx.getStorageSync('user_id'),
      switchActive = this.data.switchActive;
    that.setData({
      loading: true,
      disabled: true
    });
    if (name == '') {
      app.showToast('请填写姓名', () => {});
      that.setData({
        loading: false,
        disabled: false
      });
      return false;
    }
    if (tel == '') {
      app.showToast('请填写电话', () => {});
      that.setData({
        loading: false,
        disabled: false
      });
      return false;
    }
    if (address == '') {
      app.showToast('请选择所在地区', () => {});
      that.setData({
        loading: false,
        disabled: false
      });
      return false;
    }
    if (address_text == '') {
      app.showToast('请完善详细信息', () => {});
      that.setData({
        loading: false,
        disabled: false
      });
      return false;
    }
    if (switchActive == true) {
      is_default = 1;
    } else {
      is_default = 0;
    }
    https.post('saveAddress', {
      name: name,
      tel: tel,
      address: address,
      user_id: user_id,
      address_id: address_id,
      address_text: address_text,
      is_default: is_default,
      act: act
    }, false, '正在保存地址').then(res => {
      if (res > 0) {
        if (cart_ids != '' && cart_ids != null) {
          app.globalData.address_id = res;
          wx.navigateBack({
            delta: 1,
          })
          // wx.redirectTo({
          //   url: '/pages/goods_sure/goods_sure',
          // })
        } else {
          wx.navigateBack({});
        }
      }
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.address_id > 0) {
      that.setData({
        address_id: options.address_id,
        act: 'edit'
      })
      that.getAddressInfo();
    }
  },

  getAddressInfo: function () {
    var that = this,
      address_id = this.data.address_id;
    https.post('addressInfo', {
      address_id: address_id
    }, false, '加载中').then(res => {
      if (res.is_default == 1) {
        var switchActive = true;
      } else {
        var switchActive = false;
      }
      that.setData({
        name: res.name,
        tel: res.tel,
        address: res.address,
        address_text: res.address_text,
        switchActive: switchActive
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
    login_status = wx.getStorageSync('login_status');
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


})